package contest.sweek200;

import java.util.ArrayList;
import java.util.List;

/**
 * LeetCode1535. 找出数组游戏的赢家
 * @author code_yc
 * @version 1.0
 * @date 2020/8/3 22:38
 */
public class Solution2 {

    public int getWinner(int[] arr, int k) {
        int n = arr.length;
        if (k >= n - 1) return max(arr);
        // 将数组改造为列表
        List<Integer> list = new ArrayList<>();
        for (int num : arr) {
            list.add(num);
        }
        int c = 0;
        while (c != k) {
            if (list.get(0) > list.get(1)) {
                c++;
                int cur = list.remove(1);
                list.add(cur);
            } else {
                c = 1;
                int cur = list.remove(0);
                list.add(cur);
            }
        }
        return list.get(0);
    }

    private int max(int[] arr) {
        int res = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > res) res = arr[i];
        }
        return res;
    }

}
